package com.hardydou.framework.auth;

import org.thymeleaf.context.ITemplateContext;
import org.thymeleaf.context.WebEngineContext;
import org.thymeleaf.model.IProcessableElementTag;
import org.thymeleaf.processor.element.AbstractElementTagProcessor;
import org.thymeleaf.processor.element.IElementTagStructureHandler;
import org.thymeleaf.templatemode.TemplateMode;

import java.util.HashSet;
import java.util.Set;

/**
 * @author ：hardy
 * @description：资源依赖标签处理类
 * @date ：Created in 2020/5/30
 * @Copyright ：Hardydou.com
 */
public class AuthDataDepElementTagProcessor extends AbstractElementTagProcessor {

    private static final String TAG_NAME = "data";
    private static final int PRECEDENCE = 1000;


    public AuthDataDepElementTagProcessor(String dialectPrefix) {
        super(TemplateMode.HTML, dialectPrefix, TAG_NAME, true, null, false, PRECEDENCE);
    }

    @Override
    protected void doProcess(ITemplateContext context, IProcessableElementTag tag, IElementTagStructureHandler structureHandler) {
        String req = ((WebEngineContext) context).getRequest().getRequestURI();
        String uris = tag.getAttributeValue("uri");
        Set<String> uriSet = new HashSet<>();
        String[] tmps = uris.split(",");
        String contextPath=((WebEngineContext) context).getServletContext().getContextPath();
        for (String tmp : tmps) {
            uriSet.add(contextPath+tmp.trim());
        }
        AppAuthService.addDepRes(req, uriSet);
        structureHandler.removeTags();
    }
}
